'use strict';
// default require
var React = require('react');
var Router = require('react-router');
var RouterContainer = require('./services/RouterContainer');
var LoginActions = require('./actions/login');
var Route = Router.Route;
var DefaultRoute = Router.DefaultRoute;
var RouteHandler = Router.RouteHandler;
// extra parts you want to add to your App
var AMR = require('amazeui-react');
var Topbar = AMR.Topbar;
var Nav = AMR.Nav;
var CollapsibleNav = AMR.CollapsibleNav;
var Container = AMR.Container;

/* [COMPONENTS] */
<% if (app.components) {
underscore.each(app.components , function(path , name) { %>var <%= name %> = require('<%= path %>');
<% });
} %>
/* [COMPONENTS] */

var App = React.createClass({
  render: function() {
    return (
      <div className="ask-page">
        <Topbar
          className="ask-header"
          brand="Amaze UI"
          brandLink="/"
          inverse>
          <CollapsibleNav>
            <Nav topbar>
			<% if (app.pages) {
			underscore.each(app.pages , function(path , name) { %><RouteLink to="<%= name.toLowerCase() %>"><%= name %></RouteLink>
			<% });
			} %>
            </Nav>
          </CollapsibleNav>
        </Topbar>
        <main className="ask-main">
          <RouteHandler />
        </main>
        <SiteFooter />
      </div>
    );
  }
});

// there should always been a index file
var Index = require('./pages/index');
/* [PAGES] */
<% if (app.pages) {
underscore.each(app.pages , function(path , name) { %>var <%= name %> = require('<%= path %>');
<% });
}%>
/* [PAGES] */

/* [ROUTES] */
var routes = (
  <Route name="app" path="/" handler={App}>
    <DefaultRoute name="index" handler={Index} />
	<% if (app.pages) {
underscore.each(app.pages , function(path , name) { %><Route name='<%= name.toLowerCase() %>' handler={<%= name %>} />
	<% });
	} %>
  </Route>
);
/* [ROUTES] */

/**
 * additional code for the jwt-auth
 */
var router = Router.create({routes});
RouterContainer.set(router);

var jwt = localStorage.getItem('jwt');
if (jwt) {
    LoginActions.loginUser(jwt);
}
/**
 * render to the page
 */
document.addEventListener('DOMContentLoaded', function() {
    Router.run(routes,
    function(Handler) {
      React.render(<Handler />, document.body);
    });
});

// -- EOF -- //
